//
// Created by gastonfeng on 2022/12/19.
//

#ifndef RTE_TM2831_LCD_AWTK_H
#define RTE_TM2831_LCD_AWTK_H

#include <cstring>
#include "smodule.h"
#include "gui_type.h"
#include "firmata.pb.h"
#include "pb_common.h"

class lcd_awtk : public smodule {
    int run(uint32_t tick) final;

    int begin(uint32_t tick) final;

    const char *name() final {
        return "lcd";
    }

    int type() override {
        return pb_object_lcd_tag;
    }

    int encode(pb_object *msg, pb_ostream_t *stream) override {
        msg->object.lcd = data;
        msg->which_object = pb_object_lcd_tag;
        return pb_encode(stream, pb_object_fields, msg);
    }

    int iter(pb_field_iter_t *pS) override {
        return pb_field_iter_begin(pS, pb_object_fields, &data);
    }

    int diag(uint32_t tick) final;

#ifdef __PLATFORMIO_BUILD_DEBUG__
    int dev_test(uint32_t tick) final;
#endif

public:
    static int loadui(void *ui_dat);

    static _widget_t *widgetCreate(widget_t *pWidget, nWidget_t *widget);

    int touch_calibrate();

    nGui_t *dat;
    pb_lcd_info data;
    int current_win = -1;

    static void hmi_set_win(int winitem);

    nWin_t *win;

    static unsigned char hmi_get_button_value(_widget_t *pWidget);

    static void hmi_set_button_value(_widget_t *pWidget, unsigned char i);

    static void hmi_add_assert(const void *str);
};

extern lcd_awtk awtk;
#endif // RTE_TM2831_LCD_AWTK_H
